import java.util.*;
import org.junit.*;
import java.text.DateFormat;
import static org.junit.Assert.*;
import org.joda.time.*;

/* good luck, have fun
 */

public class TestScheduleProDataConnection {
    ScheduleProDataConnection sproData;

    @Before public void initialize() {
	sproData = new ScheduleProDataConnection(
		"jdbc:mysql://411green.cs.odu.edu:3306/scheduler_test_data",
		"411green",
		"fpjcsa");
    }

    @Test public void checkConnection() {
	assertTrue(sproData.getMessage(), sproData.isGood());
    }

    @Test public void getResourceListTest() {
	// Gets the invite list for a New Event
	ArrayList<Integer> resourceList = sproData.getResourceRequestList(1);
	assertFalse("Failure, Resource not found",resourceList==null);
       
	for(int i = 1; i <= 1; i++) {
	    assertTrue("Incorrect resource list", resourceList.get(i).intValue()==i);
	}
    }

    @Test public void getInviteListTest() {
	// Gets the invite list for a New Event
	ArrayList<Integer> inviteList = sproData.getInviteList(1);
	for(int i = 1; i <= 3; i++) {
	    assertTrue("Incorrect invitation list", inviteList.get(i-1).intValue()==i);
	}
    }

    @Test public void schedulerTestPrivateEvents() {
	System.out.println("Running scheduler");
	Scheduler scheduler = new Scheduler(2, sproData);

	ValidEvent ev = scheduler.getBestValidEvent();

	assertTrue("received null...", ev != null);

	DateTime retStart = new DateTime(ev.start);
	DateTime retStop = new DateTime(ev.end);

	DateTime start = new DateTime(2015, 2, 2, 11, 0, 0);
	DateTime stop = new DateTime(2015, 2, 2, 12, 0, 0);

	assertTrue("Expected " + start + ", got " + retStart, ev.start == start.getMillis());
	assertTrue("Expected " + stop + ", got " + retStop, ev.end == stop.getMillis());
    }

    @Test public void getInviteePrivateEventsTest() {
	HashMap<String, Event> privateEventList = sproData.getInviteePrivateEvents(1);
	assertTrue(privateEventList.get("P1_0") != null);
	assertTrue(privateEventList.get("P1_1") != null);
	assertTrue(privateEventList.get("P1_2") != null);
	assertTrue(privateEventList.get("P2_0") != null);
	assertTrue(privateEventList.get("P3_0") != null);
    }

    @Test public void getInviteeEventsTest() {
	DateTime start = new DateTime(2015, 1, 1, 8, 0);
	HashMap<String, Event> eventList = sproData.getInviteeEvents(1); // get the list of Events associated with the invitees of new_event
	assertFalse("eventList returned is null", eventList==null);
	Event e1 = eventList.get("E1");
	Event e3 = eventList.get("E3");
	assertFalse("E1 is null", e1 == null);
	assertFalse("E3 is null", e3 == null);
	DateTime rcv = new DateTime(e1.start);
	assertTrue("Incorrect invtitee event (E1) returned, got '" + e1.id +"'", e1.id.equals("E1"));
	assertTrue("Incorrect invtitee event (E3) returned, got '" + e3.id +"'", e3.id.equals("E3"));
	assertTrue("Wrong event start time, expected " + start.toString() + " got " + rcv.toString(), start.getMillis() == e1.start);
    }

    // gets the list of invitees (not just the invite list).
    @Test public void getInviteeListTest() {
	HashMap<String, Event> eventList = sproData.getInviteeEvents(1);
	ArrayList<Invitee> inviteeList = sproData.getInvitees(1, eventList); // get the list of Invitee objects of a new_event
	assertTrue(inviteeList.get(0).id.equals("I1"));
	assertTrue(inviteeList.get(1).id.equals("I2"));
	assertTrue(inviteeList.get(2).id.equals("I3"));
    }

    @Test public void inviteesLinkedWithPrivateTest() {
	HashMap<String, Event> eventList = sproData.getInviteeEvents(1);
	eventList.putAll(sproData.getInviteePrivateEvents(1));
	ArrayList<Invitee> inviteeList = sproData.getInvitees(1, eventList); // get the list of Invitee objects of a new_event
	Invitee i = inviteeList.get(0);
	for(int pIDCounter = 0; pIDCounter < 3; pIDCounter++) {
	    String pID = "P1_"+pIDCounter;
	    assertTrue("Invitee 1 private links...", i.schedule.contains(eventList.get(pID)));
	}		       
	i = inviteeList.get(1);
	assertTrue("Invitee 2 private links...", i.schedule.contains(eventList.get("P2_0")));
	i = inviteeList.get(2);	
	assertTrue("Invitee 3 private links...", i.schedule.contains(eventList.get("P3_0")));
    }

    @Test public void getEventTest() {
	DateTime start = new DateTime(2015, 1, 1, 8, 0);

	NewEvent ev = sproData.getNewEventEntry(1);
	assertTrue(sproData.getNewEventEntry(0)==null);
	assertTrue("No event found ", ev != null);
	assertTrue("new_event_id == 1", (ev.getId()==1));
	assertTrue("new_event_name==TestEvent0001", 
		   (ev.getName().equals("TestEvent0001")));	
	assertTrue("ev.getStart: got " + ev.getStart().toString(),ev.getStart().equals(start));
	assertTrue("ev.getLength:",ev.getLength()==(2*60*60*1000));
    }

    @After public void dispose() {
	sproData.close();
    }
}
